package com.example.kotlin_library

import androidx.compose.foundation.layout.*
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

/**
 * UniversalAsyncImage 在 Android 上的测试示例
 */
@Composable
fun UniversalAsyncImageTestSample() {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp),
        verticalArrangement = Arrangement.spacedBy(16.dp)
    ) {
        Text(
            text = "UniversalAsyncImage 测试",
            fontSize = 20.sp,
            color = Color.Black
        )

        // 示例1: 基本用法 - 网络 WebP 图片
        Text("示例1: 网络 WebP 图片", fontSize = 14.sp)
        UniversalAsyncImage(
            url = "https://i-blog.csdnimg.cn/direct/0053b18545c84151a8ba37243803cdff.webp",
            contentDescription = "网络图片",
            modifier = Modifier
                .fillMaxWidth()
                .height(200.dp)
        )

        // 示例2: 带加载状态的图片
        Text("示例2: 带加载状态", fontSize = 14.sp)
        UniversalAsyncImageWithLoadingState(
            url = "https://picsum.photos/400/300"
        )

        // 示例3: 小尺寸图片
        Text("示例3: 小尺寸图片", fontSize = 14.sp)
        Row(
            horizontalArrangement = Arrangement.spacedBy(8.dp)
        ) {
            UniversalAsyncImage(
                url = "https://picsum.photos/100/100",
                contentDescription = "小图1",
                modifier = Modifier.size(100.dp)
            )
            UniversalAsyncImage(
                url = "https://picsum.photos/100/100?random=2",
                contentDescription = "小图2",
                modifier = Modifier.size(100.dp)
            )
            UniversalAsyncImage(
                url = "https://picsum.photos/100/100?random=3",
                contentDescription = "小图3",
                modifier = Modifier.size(100.dp)
            )
        }
    }
}

/**
 * 带加载状态的 UniversalAsyncImage 示例
 */
@Composable
fun UniversalAsyncImageWithLoadingState(url: String) {
    var isLoading by remember { mutableStateOf(false) }
    var hasError by remember { mutableStateOf(false) }

    Box(
        modifier = Modifier
            .fillMaxWidth()
            .height(200.dp),
        contentAlignment = Alignment.Center
    ) {
        UniversalAsyncImage(
            url = url,
            contentDescription = "示例图片",
            modifier = Modifier.fillMaxSize(),
            onLoading = {
                isLoading = true
                hasError = false
                android.util.Log.d("UniversalAsyncImage", "开始加载: $url")
            },
            onSuccess = {
                isLoading = false
                hasError = false
                android.util.Log.d("UniversalAsyncImage", "加载成功: $url")
            },
            onError = { error ->
                isLoading = false
                hasError = true
                android.util.Log.e("UniversalAsyncImage", "加载失败: $url", error)
            }
        )

        // 加载指示器
        if (isLoading) {
            CircularProgressIndicator(
                color = Color.Blue
            )
        }

        // 错误提示
        if (hasError) {
            Text(
                text = "图片加载失败",
                color = Color.Red,
                fontSize = 16.sp
            )
        }
    }
}
